""""
@file: redis.py
@author: lingdubing
@time: 2024/11/05  9:53
@desc: redis数据库配置
@character: utf-8
@exp: "如遇到超时错误，查看这篇文章https://blog.csdn.net/ViniJack/article/details/131809573"
"""

import aioredis
import os
from aioredis import Redis
from config import settings


async def sys_cache() -> Redis:
    """
    骑行状态缓存
    :return: cache 连接池
    """
    # 从URL方式创建redis连接池
    sys_cache_pool = aioredis.ConnectionPool.from_url(
        f"redis://{settings.CACHE_HOST}:{settings.CACHE_PORT}",
        db=os.getenv('CACHE_DB', 0),
        encoding='utf-8',
        decode_responses=True
    )
    return Redis(connection_pool=sys_cache_pool)


async def code_cache() -> Redis:
    """
    验证码缓存
    :return: cache 连接池
    """
    # 从URL方式创建redis连接池
    sys_cache_pool = aioredis.ConnectionPool.from_url(
        f"redis://{settings.CACHE_HOST}:{settings.CACHE_PORT}",
        db=os.getenv('CACHE_DB', 1),
        encoding='utf-8',
        decode_responses=True
    )
    return Redis(connection_pool=sys_cache_pool)


async def machine_cache() -> Redis:
    """
    电动车状态缓存
    :return:
    """
    # 从URL方式创建redis连接池
    sys_cache_pool = aioredis.ConnectionPool.from_url(
        f"redis://{settings.CACHE_HOST}:{settings.CACHE_PORT}",
        db=os.getenv('CACHE_DB', 2),
        encoding='utf-8',
        decode_responses=True
    )
    return Redis(connection_pool=sys_cache_pool)
